package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityScholarshipDto;
import xyz.zhouzhaodong.crm.dto.UniversityScholarshipDto;
import xyz.zhouzhaodong.crm.entity.UniversityScholarship;
import xyz.zhouzhaodong.crm.entity.UniversityScholarship;
import xyz.zhouzhaodong.crm.entity.UniversityScholarship;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityScholarshipMapper;
import xyz.zhouzhaodong.crm.mapper.UniversityScholarshipMapper;
import xyz.zhouzhaodong.crm.service.IUniversityScholarshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校奖学金 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityScholarshipServiceImpl extends ServiceImpl<UniversityScholarshipMapper, UniversityScholarship> implements IUniversityScholarshipService {

    @Resource
    UniversityScholarshipMapper universityScholarshipMapper;

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversityScholarship(UniversityScholarshipDto universityScholarshipDto) {
        try {
            UniversityScholarship universityScholarship = dtoChange(universityScholarshipDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            universityScholarship.setId(id);
            universityScholarship.setCreateUser(universityScholarshipDto.getStaffId());
            universityScholarship.setCreateTime(new Date());
            universityScholarshipMapper.insert(universityScholarship);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversityScholarship(UniversityScholarshipDto universityScholarshipDto) {
        try {
            UniversityScholarship universityScholarship = new UniversityScholarship();
            universityScholarship.setId(universityScholarshipDto.getId());
            universityScholarship.setUpdateUser(universityScholarshipDto.getStaffId());
            universityScholarship.setUpdateTime(new Date());
            universityScholarship.setFlag(1);
            universityScholarshipMapper.updateById(universityScholarship);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversityScholarship(UniversityScholarshipDto universityScholarshipDto) {
        try {
            UniversityScholarship universityScholarship = dtoChange(universityScholarshipDto);
            universityScholarship.setUpdateUser(universityScholarshipDto.getStaffId());
            universityScholarship.setUpdateTime(new Date());
            universityScholarshipMapper.updateById(universityScholarship);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversityScholarship(UniversityScholarshipDto universityScholarshipDto) {
        Integer flag = universityScholarshipDto.getFlag() == null ? 0 : universityScholarshipDto.getFlag();
        if (universityScholarshipDto.getPageNo() != 0 && universityScholarshipDto.getPageSize() != 0){
            Page<UniversityScholarshipDto> page = new Page<>(universityScholarshipDto.getPageNo(), universityScholarshipDto.getPageSize());
            universityScholarshipDto.setFlag(flag);
            IPage<UniversityScholarshipDto> list = universityScholarshipMapper.selectListPage(page, universityScholarshipDto);
            for(UniversityScholarshipDto u : list.getRecords()){
                u.setUniversity(universityMapper.selectById(u.getUniversityId()));
            }
            return RestMessage.success(list);
        } else {
            QueryWrapper<UniversityScholarship> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("university_id", universityScholarshipDto.getUniversityId());
            queryWrapper.eq("flag", flag);
            List<UniversityScholarship> list = universityScholarshipMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    private UniversityScholarship dtoChange(UniversityScholarshipDto universityScholarshipDto){
        UniversityScholarship universityScholarship = new UniversityScholarship();
        universityScholarship.setId(universityScholarshipDto.getId());
        universityScholarship.setUniversityId(universityScholarshipDto.getUniversityId());
        universityScholarship.setType(universityScholarshipDto.getType());
        universityScholarship.setMoney(universityScholarshipDto.getMoney());
        universityScholarship.setEducation(universityScholarshipDto.getEducation());
        universityScholarship.setSuitStudent(universityScholarshipDto.getSuitStudent());
        universityScholarship.setTuition(universityScholarshipDto.getTuition());
        universityScholarship.setHotelExpense(universityScholarshipDto.getHotelExpense());
        universityScholarship.setInsurance(universityScholarshipDto.getInsurance());
        universityScholarship.setLivingExpenses(universityScholarshipDto.getLivingExpenses());
        universityScholarship.setRemark(universityScholarshipDto.getRemark());
        universityScholarship.setFlag(universityScholarshipDto.getFlag());
        return universityScholarship;
    }
    
}
